#!/bin/bash

Backup_MariaDB()
{

    echo "Starting backup all databases..."
    echo "If the database is large, the backup time will be longer."
    /usr/local/mariadb/bin/mysqldump -uroot -p${DB_Root_Password} --all-databases > /root/mariadb_all_backup${Upgrade_Date}.sql
    if [ $? -eq 0 ]; then
        echo "MariaDB databases backup successfully.";
     else
        echo "MariaDB databases backup failed,Please backup databases manually!"
        exit 1
    fi
    moodle stop
    #mv /etc/init.d/mariadb /usr/local/mariadb/mariadb-${cur_mariadb_version}
    #mv /etc/my.cnf /usr/local/mariadb/my.cnf.mariadb-${cur_mariadb_version}
    mv /usr/local/mariadb /usr/local/mariadb-${cur_mariadb_version}
}

Upgrade_MariaDB()
{

    Verify_DB_Password
    cur_mariadb_version=$( /usr/local/mariadb/bin/mysql -V | awk '{print $5}' | tr -d "\-MariaDB," )
    mariadb_version=""
    echo "Current MariaDB Version:${cur_mariadb_version}"
    echo "You can get version number from https://downloads.mariadb.org/"
    echo "Please enter MariaDB Version you want."
    read -p "(example: ${MariaDB_Version} ): " mariadb_version
    if [ "${mariadb_version}" = "" ]; then
        echo "Error: You must input MariaDB Version!!"
        exit 1
    fi
    echo "====================================================================="
    echo "You will upgrade MariaDB V${cur_mariadb_version} to V${mariadb_version}"
    echo "====================================================================="
    Press_Start
    echo "============================check files=================================="
    cd ${cur_dir}/src
    if [ -s mariadb-${mariadb_version}.tar.gz ]; then
        echo "mariadb-${mariadb_version}.tar.gz [found]"
     else
        echo "Error: mariadb-${mariadb_version}.tar.gz not found!!!download now......"
        wget -c ${Download_Mirror}/mariadb/mariadb-${mariadb_version}.tar.gz
        if [ $? -eq 0 ]; then
            echo "Download mariadb-${mariadb_version}.tar.gz successfully!"
         else
            wget -c http://mirrors.ustc.edu.cn/mariadb/mariadb-${mariadb_version}/source/mariadb-${mariadb_version}.tar.gz
            if [ $? -eq 0 ]; then
                echo "Download mariadb-${mariadb_version}.tar.gz successfully!"
             else
                wget -c https://downloads.mariadb.org/interstitial/mariadb-${mariadb_version}/kvm-tarbake-jaunty-x86/mariadb-${mariadb_version}.tar.gz
                if [ $? -eq 0 ]; then
                    echo "Download mariadb-${mariadb_version}.tar.gz successfully!"
                 else
                    echo "You enter MariaDB Version was:"${mariadb_version}
                    echo -e "\033[31mError! You entered a wrong version number, please check!\033[0m"
                    sleep 5
                    exit 1
                fi
            fi
        fi
    fi

    Backup_MariaDB
    
    echo "Starting upgrade MariaDB..."
    tar zxf mariadb-${mariadb_version}.tar.gz
    cd mariadb-${mariadb_version}
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb -DWITH_ARIA_STORAGE_ENGINE=1 -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 ${MariaDBMAOpt}
    make && make install

    /etc/init.d/mariadb start
    if [ $? -eq 0 ]; then
        echo "MariaDB databases import successfully."
        /etc/init.d/mariadb restart
     else
        echo "MariaDB databases import failed,Please import databases manually!"
        if [ -d "${DB_Data_Dir}" ]; then
            mv ${DB_Data_Dir} ${DB_Data_Dir}${Upgrade_Date}
        fi
        mkdir -p ${DB_Data_Dir}
        chown -R mariadb:mariadb ${DB_Data_Dir}
        /usr/local/mariadb/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mariadb --datadir=${DB_Data_Dir} --user=mariadb
        /usr/local/mariadb/bin/mysqladmin -u root password ${DB_Root_Password}
        cat > /tmp/mariadb_sec_script<<EOF
use mysql;
update user set password=password('${DB_Root_Password}') where user='root';
delete from user where not (user='root') ;
delete from user where user='root' and password=''; 
drop database test;
DROP USER ''@'%';
flush privileges;
EOF
        /usr/local/mariadb/bin/mysql -u root -p${DB_Root_Password} -h localhost < /tmp/mariadb_sec_script
        rm -f /tmp/mariadb_sec_script
        /etc/init.d/mariadb restart
        echo "Use: mysql -u root -p${DB_Root_Password} < /root/mariadb_all_backup${Upgrade_Date}.sql"
    fi
    echo "Repair databases..."
    /usr/local/mariadb/bin/mysql_upgrade -u root -p${DB_Root_Password}
    /etc/init.d/mariadb stop
    moodle start
    if [[ -s /usr/local/mariadb/bin/mysql && -s /usr/local/mariadb/bin/mysqld_safe && -s /etc/my.cnf ]]; then
        echo -e "\033[32m======== upgrade MariaDB completed ======\033[0m"
     else
        echo -e "\033[31m======== upgrade MariaDB failed ======\033[0m"
        echo -e "\033[31mupgrade MariaDB log: /root/upgrade_mariadb.log\033[0m"
        echo "You mail upgrade_mariadb.log to hongm@sina.com Forum for help."
    fi
    rm -fr ${cur_dir}/src/mariadb-${mariadb_version}
}
